<?php

namespace app\mp\admin;

use app\common\service\OrderMaterial as OrderMaterialService;
use app\system\admin\Admin;
use app\common\service\Order as OrderService;
use app\common\service\Material as MaterialService;
use app\common\service\DistributingFeed as DistributingFeedService;

/**
 * 补料任务
 * @author King
 *
 */
class DistributingFeedV2 extends Admin
{

    protected $hisiModel = 'DistributingFeed';//模型名称[通用添加、修改专用]
    protected $hisiValidate = 'DistributingFeed';
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 开始采购
     *
     *
     * @author KING
     * @date 2020年6月3日
     *
     */
    public function startPurchasing()
    {
        $materialId = $this->request->param('material_id/d', 0);
        $orderId = $this->request->param('order_id/d', 0);
        $orderMaterialService = new OrderMaterialService();
        $result = $orderMaterialService->startPurchasing($this, $orderId, $materialId);
        return $this->Json($result['ret'], $result['msg'], $result['data']);
    }

    /**
     * 开始采购
     * @author zhuguosen 905502314@qq.com
     * @date 2022/6/25
     */
    public function startPurchasingV2()
    {
        $mid = $this->request->param('mid/d', 0);
        $materialId = $this->request->param('material_id/d', 0);
        $orderId = $this->request->param('order_id/d', 0);
        $info = db('order_material om')->field("om.mid,om.order_id,om.client_ordernum,om.material_id,om.stock_m_type
        ,m.name
        ,om.arrive_time,om.arrive_time_first,om.is_caigou_start ")
            ->join('order o', "om.order_id=o.id", 'left')
            ->join('material m', "om.material_id=m.id", 'left')
            ->where([
                ['om.mid', '=', $mid],
            ])
            ->find();
        $orderMaterialService = new OrderMaterialService();
        $result = $orderMaterialService->startPurchasing($this, $orderId, $materialId, $info);
        return $this->Json($result['ret'], $result['msg'], $result['data'] ?? []);
    }
}